package com.hsy.crm.web.service.asyn.marketprofit;

import com.hsy.crm.web.domain.entity.Message;
import com.hsy.crm.web.domain.entity.OrderDetail;
import com.hsy.crm.web.domain.entity.SaleCostDetail;
import com.hsy.crm.web.domain.entity.StoreProductDetail;
import com.hsy.crm.web.mapper.OrderDetailMapper;
import com.hsy.crm.web.mapper.SaleCostDetailMapper;
import com.hsy.crm.web.mapper.StoreProductDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProfitCommonService {

    @Autowired
    SaleCostDetailMapper saleCostDetailMapper;

    @Autowired
    StoreProductDetailMapper storeProductDetailMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    /**
     * 回滚销售明细利润处理
     * @param message
     * @param storeProductDetail
     * @param delete  删除或者清除out数据
     */
    public void clearOutStoreProductDetail(Message message,StoreProductDetail storeProductDetail,boolean delete){
        List<SaleCostDetail> saleCostDetails = this.getOutSaleCostDetail(message,storeProductDetail);

        for( SaleCostDetail saleCostDetail : saleCostDetails ){
            this.updateStoreProductDetail(message,saleCostDetail,saleCostDetail.getOutStoreProductDetailId());
            this.updateStoreProductDetail(message,saleCostDetail,saleCostDetail.getInStoreProductDetailId());
        }

        // 销售单有SaleCostDetail则找到了成本或者部分成本   没有SaleCostDetail则没有成本  也需要清理
        storeProductDetail.setCustomerId(message.getCustomerId());
        storeProductDetailMapper.clearTmp(storeProductDetail);

        if( delete ){
            storeProductDetail.setCustomerId(message.getCustomerId());
            storeProductDetailMapper.deleteByPrimaryKey(storeProductDetail);
        }
    }

    public List<SaleCostDetail> getOutSaleCostDetail(Message message,StoreProductDetail storeProductDetail){
        SaleCostDetail detail = new SaleCostDetail();
        detail.setCustomerId(message.getCustomerId());
        detail.setOutStoreProductDetailId(storeProductDetail.getId());
        return saleCostDetailMapper.select(detail);
    }

    public List<SaleCostDetail> getInSaleCostDetail(Message message,StoreProductDetail storeProductDetail){
        SaleCostDetail detail = new SaleCostDetail();
        detail.setCustomerId(message.getCustomerId());
        detail.setInStoreProductDetailId(storeProductDetail.getId());
        return saleCostDetailMapper.select(detail);
    }

    public void updateStoreProductDetail(Message message,SaleCostDetail saleCostDetail,Long detailId){
        StoreProductDetail detail = new StoreProductDetail();
        detail.setRelationStartSort(-1L);
        detail.setRelationEndSort(-1L);
        detail.setCustomerId(message.getCustomerId());
        detail.setOutNumber(-saleCostDetail.getOutNumber());
        detail.setOutPrice(-saleCostDetail.getOutPrice());
        detail.setId(detailId);
        detail.setTmpPrice(-2l);
        storeProductDetailMapper.addOut(detail);

        saleCostDetail.setCustomerId(message.getCustomerId());
        saleCostDetailMapper.deleteByPrimaryKey(saleCostDetail);
    }

    public Long getOrderDetailId(Message message,StoreProductDetail storeProductDetail){
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setCustomerId(message.getCustomerId());
        orderDetail.setId(storeProductDetail.getOrderDetailId());
        orderDetail = orderDetailMapper.selectByPrimaryKey(orderDetail);
        if( orderDetail == null ) return -1L;
        return orderDetail.getOrderDetailId();
    }


}
